<template>
	<om-formsub-outer :index="index" :customAddFunc="true" :customDelFunc="true"  @save="save" @delete="del">
		<uni-card :titleUnderline="false" :is-full="true" style="border: none;border-radius: 1px;" titleBgColor="#eee" :title="'明细(' + order + ')'">
			<uni-list>
				<uni-list-item :showArrow="!checkedit">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="计划项" required></om-label></view>
						<view v-if="checkedit" class="tui-right tui-col-7">{{ formdata.planItemName }}</view>
						<view v-else class="tui-right tui-col-7">
							<picker mode="selector" @change="planItemFunc" :range="planItemList" :value="formdata.planItemIndex" range-key="dicname">
								<view>{{ ~formdata.planItemIndex && planItemList.length ? planItemList[formdata.planItemIndex].dicname : '请选择' }}</view>
							</picker>
						</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="!checkedit">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="指标名称" required></om-label></view>
						<view v-if="checkedit" class="tui-right tui-col-7">{{ formdata.indexDictName }}</view>
						<view v-else class="tui-right tui-col-7">
							<picker mode="selector" @change="initIndexFunc" :range="indexDictList" :value="formdata.indexDictIndex" range-key="dicname">
								<view>{{ ~formdata.indexDictIndex && indexDictList.length ? indexDictList[formdata.indexDictIndex].dicname : '请选择' }}</view>
							</picker>
						</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="!checkedit">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="运营指标范围" :required="indexCategoryList.length ? true : false"></om-label></view>
						<view v-if="checkedit" class="tui-right tui-col-7">{{ formdata.indexCategoryName }}</view>
						<view v-else class="tui-right tui-col-7">
							<picker mode="selector" @change="indexCategoryFunc" :range="indexCategoryList" :value="formdata.indexCategoryIndex" range-key="indexCategoryName">
								<view>
									{{ ~formdata.indexCategoryIndex && indexCategoryList.length ? indexCategoryList[formdata.indexCategoryIndex].indexCategoryName : '请选择' }}
								</view>
							</picker>
						</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="本人执行数" required></om-label></view>
						<view class="tui-col-7"><input type="digit" @blur="blurSum" v-model="formdata.sum" placeholder="请输入数字" /></view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="计量单位"></om-label></view>
						<view class="tui-col-7">{{ formdata.indexUnitName }}</view>
					</view>
				</uni-list-item>
				<uni-list-item :showArrow="false">
					<view class="tui-flex">
						<view class="tui-col-5"><om-label label="说明"></om-label></view>
						<view v-if="checkedit" class="tui-right tui-col-7">{{ formdata.remark }}</view>
						<view v-else class="tui-left tui-col-7"><textarea :auto-height="true" class="om-textarea" v-model="formdata.remark" placeholder="请输入..." /></view>
					</view>
				</uni-list-item>
			</uni-list>
		</uni-card>
	</om-formsub-outer>
</template>

<script>
import { loadZpos } from '../../../static/js/formOld.js';
import { isNotEmpty } from '@/static/js/common.js';
import { mapState } from 'pinia';
import {useLoginUserStore} from '@/stores/login-user.js';
const form = require('../../../static/js/formValidation.js');
export default {
	data() {
		return {
			checkedit: false,
			cachename: '',
			index: null,
			datas: [],
			order: 0,
			modifyInitFlag: false, //修改初始标识
			formdata: {
				id: '',
				planItemIndex: 0,
				planItemId: '', //计划项
				planItemName: '',
				indexDictIndex: -1,
				indexDictId: '', //指标名称(IndexDict)
				indexDictName: '',
				customCateId: '', //自定义类别(Tsysdict)
				inputPrecision: '', //指标输入精度（小数位数）
				userId: '', //指标负责人
				userName: '',
				postId: '', //指标承担岗位
				postName: '',
				instId: '', //指标承担机构
				instName: '',
				indexCategoryIndex: -1,
				indexCategoryName: '', //运营指标范围(根据承担岗位获取范围)
				indexCategoryRequired: '',
				sum: '', //数量
				indexUnitId: '', //计量单位(Tsysdict)
				indexUnitName: '',
				remark: '' //说明
			},
			zposId: '',
			reportTime: '',
			// year: '',
			// month: '',
			modelUserId: '',
			modelUserName: '',
			modelPostId: '',
			modelPostName: '',
			modelInstId: '',
			modelInstName: '',
			planItemList: [],
			indexDictList: [],
			indexCategoryList: [],
			indexCacheKey: 'data-info-index-cache'
		};
	},
	watch: {},
	computed: mapState(useLoginUserStore, ['userName', 'userId']),
	methods: {
		//初始化加载选项
		initLoad() {
			let _that = this;
			uni.removeStorage({
				key: _that.cachename
			});
			//初始新增
			if (!_that.modifyInitFlag) {
				//计划项
				_that.initPlanItems(function() {
					_that.planItemFunc();
				});
			} else {
				//修改;
				//计划项
				_that.initPlanItems(function() {
					if (_that.formdata.planItemId) {
						let res = _that.planItemList;
						let flag = true;
						for (let i = 0; i < res.length; i++) {
							if (res[i].id == _that.formdata.planItemId) {
								flag = false;
								_that.formdata.planItemIndex = i;
								_that.formdata.planItemName = res[i].dicname;
							}
						}
						if (flag) {
							_that.om.toast('未找到符合的计划项');
							_that.formdata.planItemIndex = -1;
							_that.formdata.planItemId = '';
							_that.formdata.planItemName = '';
						}
					}	
					
				});
				//指标
				_that.initIndexs(function() {
					if (_that.formdata.indexDictId) {
						let res = _that.indexDictList;
						let flag = true;
						for (let i = 0; i < res.length; i++) {
							if (res[i].id == _that.formdata.indexDictId) {
								flag = false;
								_that.formdata.indexDictIndex = i;
								_that.formdata.indexDictName = res[i].dicname;
							}
						}
						if (flag) {
							_that.om.toast('未找到符合的指标名称');
							_that.formdata.indexDictIndex = -1;
							_that.formdata.indexDictId = '';
							_that.formdata.indexDictName = '';
						}
					}	
				});
				//人员范围
				_that.loadCategoryt(function() {
					if (_that.formdata.indexCategoryName) {
						let res1 = _that.indexCategoryList;
						let flag1 = true;
						for (let i = 0; i < res1.length; i++) {
							if (res1[i].indexCategoryName == _that.formdata.indexCategoryName) {
								flag1 = false;
								_that.formdata.indexCategoryIndex = i;
							}
						}
						if (flag1) {
							_that.om.toast('未找到符合的运营指标范围');
							_that.formdata.indexCategoryIndex = -1;
							_that.formdata.indexCategoryName = '';
						}
					}	
				});
			}
		},

		//加载计划项
		initPlanItems(callback) {
			//计划项
			this.om.request('dictZero/getListByCode', { code: '36924' }).then(res => {
				this.planItemList = res;
				if (callback) {
					callback();
				}
			});
		},

		//计划项
		planItemFunc(e) {
			if (e) {
				//清空相关后续选项
				this.indexDictList = [];
				this.formdata.indexDictIndex = -1;
				this.formdata.indexDictId = '';
				this.formdata.indexDictName = '';
				this.formdata.customCateId = '';
				this.formdata.inputPrecision = '';
				this.formdata.userId = '';
				this.formdata.userName = '';
				this.formdata.postId = '';
				this.formdata.postName = '';
				this.formdata.instId = '';
				this.formdata.instName = '';
				this.indexCategoryList = [];
				this.formdata.indexCategoryIndex = -1;
				this.formdata.indexCategoryName = '';
				this.formdata.indexCategoryRequired = '';
				this.formdata.sum = '';
				this.formdata.indexUnitId = '';
				this.formdata.indexUnitName = '';
				this.formdata.remark = '';
				this.formdata.planItemIndex = e.target.value;
			}
			let index = this.formdata.planItemIndex;
			let list = this.planItemList;
			if (~index && list.length) {
				this.formdata.planItemId = list[index].id;
				this.formdata.planItemName = list[index].dicname;
				this.initIndexs();
			}
		},

		//根据计划项获取指标选项
		initIndexs(callback) {
			let _that = this;
			let zposId = _that.zposId;
			let reportTime = _that.reportTime;
			let userId = _that.modelUserId;
			let postId = _that.modelPostId;
			let planItemId = _that.formdata.planItemId;
			if (zposId && reportTime && planItemId) {
				_that.om
					.request('/anyTimeReport/findIndexDict', {
						zposId: zposId,
						reportTime: reportTime,
						userId: userId,
						postId: postId,
						planItemId: planItemId
					})
					.then(res => {
						if (res) {
							_that.indexDictList = res;
							// if (_that.indexDictList.length == 1 && _that.formdata.indexDictIndex == -1) {
							// 	_that.formdata.indexDictIndex = 0;
							// }
						} else {
							_that.indexDictList = [];
						}
						if (callback) {
							callback();
						} else {
							_that.initIndexFunc();
						}
					});
			}
		},

		//指标名称
		initIndexFunc(e) {
			if (e) {
				//清空相关后续选项
				this.formdata.userId = '';
				this.formdata.userName = '';
				this.formdata.postId = '';
				this.formdata.postName = '';
				this.formdata.instId = '';
				this.formdata.instName = '';
				this.indexCategoryList = [];
				this.formdata.indexCategoryIndex = -1;
				this.formdata.indexCategoryName = '';
				this.formdata.indexCategoryRequired = '';
				this.formdata.sum = '';
				this.formdata.indexUnitId = '';
				this.formdata.indexUnitName = '';
				this.formdata.remark = '';
				if (this.planItemList.length && e.target.value == -1) {
					this.formdata.indexDictIndex = 0;
				} else {
					this.formdata.indexDictIndex = e.target.value;
				}
			}
			let index = this.formdata.indexDictIndex;
			let list = this.indexDictList;
			if (~index && list.length) {
				var indexDict = list[index];
				this.formdata.indexDictId = indexDict.id;
				this.formdata.indexDictName = indexDict.dicname;
				this.formdata.inputPrecision = indexDict.inputPrecision;
				this.formdata.indexUnitId = indexDict.indexUnitId;
				this.formdata.indexUnitName = indexDict.indexUnitName;
				this.formdata.userId = this.modelUserId;
				this.formdata.userName = this.modelUserName;
				this.formdata.postId = this.modelPostId;
				this.formdata.postName = this.modelPostName;
				this.formdata.instId = this.modelInstId;
				this.formdata.instName = this.modelInstName;
				this.loadCategoryt();
			}
		},

		//根据岗位获取其他可选项
		loadCategoryt(callback) {
			let _that = this;
			let postId = _that.formdata.postId;
			let indexDictId = _that.formdata.indexDictId;
			let reportTime = _that.reportTime;
			let planItemId = _that.formdata.planItemId;
			if (postId && reportTime && planItemId && indexDictId) {
				_that.om
					.request('/anyTimeReport/getOtherOptions', {
						postId: postId,
						reportTime: reportTime,
						indexDictId: indexDictId,
						planItemId: planItemId
					})
					.then(res => {
						if (res.succeed) {
							let value = res.value ? res.value : {};
							_that.indexCategoryList = value.category ? value.category : [];
							_that.formdata.indexCategoryRequired = _that.indexCategoryList && _that.indexCategoryList.length ? true : false;
							if (_that.indexCategoryList.length == 1 && _that.formdata.indexCategoryIndex == -1) {
								_that.formdata.indexCategoryIndex = 0;
							}
							if (callback) {
								callback();
							} else {
								_that.indexCategoryFunc();
							}
						} else {
							_that.om.toast(res.msg);
						}
					});
			}
		},

		//运营指标范围
		indexCategoryFunc(e) {
			if (e) {
				// this.formdata.sum = '';
				// this.formdata.remark = '';
				if (this.indexCategoryList.length && e.target.value == -1) {
					this.formdata.indexCategoryIndex = 0;
				} else {
					this.formdata.indexCategoryIndex = e.target.value;
				}
			}
			let index = this.formdata.indexCategoryIndex;
			let list = this.indexCategoryList;
			if (~index && list.length) {
				this.formdata.indexCategoryName = list[index].indexCategoryName;
			}
		},

		//本人执行数 (根据精度自动四舍五入转换执行数)
		blurSum(e) {
			if (e) {
				let num= e.mp.detail.value
				if(isNotEmpty(num)){
					this.formdata.sum = _.round(num, this.formdata.inputPrecision);
				}else{
					this.formdata.sum = '';
				}	
			}
		},

		save() {
			let _that = this;
			let formdata = this.formdata;
			//表单规则
			let rules = [
				{
					name: 'planItemId',
					rule: ['required'],
					msg: ['请选择计划项']
				},
				{
					name: 'indexDictId',
					rule: ['required'],
					msg: ['请选择指标名称']
				},
			];

			if (this.indexCategoryList.length > 0) {
				rules.push({
					name: 'indexCategoryName',
					rule: ['required'],
					msg: ['请选择运营指标范围']
				});
			}
			let checkRes = form.validation(formdata, rules);
			if (!checkRes) {
				if (this.index != null) {
					Object.assign(this.datas[this.index], this.formdata);
				} else {
					this.datas.push(this.formdata);
				}
				uni.setStorage({
					key: this.cachename,
					data: this.datas
				});
				uni.navigateBack();
			} else {
				_that.om.toast(checkRes);
			}
		},
		del() {
			if (this.index != null) {
				this.datas.splice(this.index, 1);
			}
			uni.setStorage({
				key: this.cachename,
				data: this.datas
			});
			uni.navigateBack();
		}
	},
	onLoad(e) {
		let index = e.index;
		let _that = this;
		if (e.checkedit) {
			if (typeof e.checkedit == 'string') {
				if (e.checkedit == 'true') {
					this.checkedit = true;
				}
			} else {
				this.checkedit = e.checkedit;
			}
		}

		if (e.cachename) {
			this.cachename = e.cachename;
			this.zposId = e.zposId;
			this.reportTime = e.reportTime;
			this.modelUserId = e.modelUserId;
			this.modelUserName = e.modelUserName;
			this.modelPostId = e.modelPostId;
			this.modelPostName = e.modelPostName;
			this.modelInstId = e.modelInstId;
			this.modelInstName = e.modelInstName;
			uni.getStorage({
				key: this.cachename,
				success: function(res) {
					if (res.data && res.data.length) {
						_that.datas = res.data;
						if (isNotEmpty(index)) {
							_that.modifyInitFlag = true;
							var d = res.data[index];
							Object.assign(_that.formdata, d);
							_that.order = parseInt(index) + 1;
							_that.index = parseInt(index);
							_that.initLoad();
						} else {
							_that.order = res.data.length + 1;
							_that.initLoad();
						}
					} else {
						_that.order = 1;
						_that.initLoad();
					}
				},
				fail() {
					_that.order = 1;
					_that.initLoad();
				}
			});
		} else {
			uni.navigateBack({});
		}
	},
	onShow() {}
};
</script>
<style></style>
